package sm.upgrade.util;

import java.util.Arrays;
import java.util.List;

import sm.util.Util;

public class KeyValueChangePolicy {
	private Object[] oldKeys;

	private Object[] newKeys;

	public void setKeys(List<String> keyStrs, Object[] keys) {
		int length = keyStrs.size();
		for (int i = 0; i < length; i++) {
			String keyStr = keyStrs.get(i);
			Object[] key = Util.parseKey(keyStr);
			keys[i] = Arrays.asList(key);
		}
	}

	public void setOldKeys(List<String> oldKeys) {
		this.oldKeys = new Object[oldKeys.size()];
		setKeys(oldKeys, this.oldKeys);
	}

	public void setNewKeys(List<String> newKeys) {
		this.newKeys = new List[newKeys.size()];
		setKeys(newKeys, this.newKeys);
	}

	public List<Object> getNewKey(List<Object> oldKey) {
		Object newKey = null;
		int length = oldKeys.length;
		for (int i = 0; i < length; i++) {
			Object oldChangeKey = oldKeys[i];
			if (oldChangeKey.equals(oldKey)) {
				newKey = newKeys[i];
				break;
			}
		}
		return (List<Object>) newKey;
	}

	public List<Object> getOldKey(List<Object> newKey) {
		Object oldKey = null;
		int length = newKeys.length;
		for (int i = 0; i < length; i++) {
			Object newChangeKey = newKeys[i];
			if (newChangeKey.equals(newKey)) {
				oldKey = oldKeys[i];
				break;
			}
		}
		return (List<Object>) oldKey;
	}
}
